<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width, initial-scale=1">
    <title>Golang Demo</title>

    <!-- Google Font: Source Sans Pro -->
    <link rel="stylesheet" href="https://fonts.googleapis.com/css?family=Source+Sans+Pro:300,400,400i,700&display=fallback">
    <!-- Font Awesome -->
    <link rel="stylesheet" href="/source/plugins/fontawesome-free/css/all.min.css">
    <!-- icheck bootstrap -->
    <link rel="stylesheet" href="/source/plugins/icheck-bootstrap/icheck-bootstrap.min.css">
    <!-- Theme style -->
    <link rel="stylesheet" href="/source/dist/css/adminlte.min.css">
</head>
<body class="hold-transition login-page">
<div class="login-box">
    <!-- /.login-logo -->
    <div class="card card-outline card-primary">
        <div class="card-header text-center">
            <a href="#" class="h1">Golang Demo</a>
        </div>
        <div class="card-body">
            <p class="login-box-msg">Sign in to start your session</p>

            <form action="#" method="post">
                <div class="input-group mb-3">
                    <input type="text" class="form-control" id="uname" placeholder="uname">
                    <div class="input-group-append">
                        <div class="input-group-text">
                            <span class="fas fa-user"></span>
                        </div>
                    </div>
                </div>
                <div class="input-group mb-3">
                    <input type="password" class="form-control" id="passwd" placeholder="Password">
                    <div class="input-group-append">
                        <div class="input-group-text">
                            <span class="fas fa-lock"></span>
                        </div>
                    </div>
                </div>
                <div class="row">
                    <div class="col-8">
                        <div class="icheck-primary">
                            <input type="checkbox" id="remember">
                            <label for="remember">
                                Remember Me
                            </label>
                        </div>
                    </div>
                    <!-- /.col -->

                    <!-- /.col -->
                </div>
            </form>



            <div class="social-auth-links text-center mt-2 mb-3">
                <a href="#" class="btn btn-block btn-primary" id="login_btn">
                    <i class="fa fa-lock mr-2"></i> Sign in
                </a>

            </div>
            <!-- /.social-auth-links -->
            <p class="mb-1" style="color: red;display: none;font-size: 13px" id="err">

            </p>
            <p class="mb-1">
                <a href="#">I forgot my password</a>
            </p>

        </div>
        <!-- /.card-body -->
    </div>
    <!-- /.card -->
</div>
<!-- /.login-box -->

<!-- jQuery -->
<script src="/source/plugins/jquery/jquery.min.js"></script>
<!-- Bootstrap 4 -->
<script src="/source/plugins/bootstrap/js/bootstrap.bundle.min.js"></script>
<!-- Golang Demo App -->
<script src="/source/dist/js/adminlte.min.js"></script>
</body>
</html>

<script type="text/javascript">

    $('#login_btn').click(function () {

        login();
    })

    $('#passwd').keydown(function(event){

        if(event.keyCode == 13){
            login();
        }
    })



    function login(){

        $('#err').html('').hide();
        var uname = $.trim($('#uname').val());
        var passwd = $.trim($('#passwd').val());

        if(!uname || !passwd){
            $('#err').html("用户名或密码错误").show();
            return;
        }

        $('#err').html('').hide();
        $.ajax({

            url:'/login',
            type:'post',
            beforeSend:function () {},
            contentType:"application/json;charset=utf-8",
            data:JSON.stringify({
                uname:uname,
                passwd:passwd
            }),


            dataType:"json",
            success:function(rs){

                if(rs.sta == 1){
                    window.location.href="/dashboard";
                }else{

                    $('#err').html(rs.msg).show();
                }

            },
            error:function(){
                $('#err').html("system error").show();
            }

        })


    }



</script>
